package com.wmlt.service.impl;

import com.wmlt.util.JdbcUtil;
import com.wmlt.dao.LiveBroadcastDao;
import com.wmlt.dao.impl.LiveBroadcastDaoImpl;
import com.wmlt.dao.impl.UserDaoImpl;
import com.wmlt.entity.LiveBroadcast;
import com.wmlt.entity.User;
import com.wmlt.service.LiveBroadcastService;
import com.wmlt.util.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * create by huangziqideveloper@gmail.com
 */
public class LiveBroadcastServiceImpl implements LiveBroadcastService {
    LiveBroadcastDao liveBroadcastDao = new LiveBroadcastDaoImpl();
    LiveBroadcast liveBroadcast = null;
    int result = 0;
    Connection connection = null;

    @Override
    public LiveBroadcast getLiveBroadcastByKey(String key) {
        return liveBroadcastDao.getLiveBroadcastByKey(key);
    }

//    @Override
//    public int deleteLiveBroadcastByKey(String key) {
//        Connection connection = JdbcUtil.getConnection();
//        try {
//            connection.setAutoCommit(false);//设置手动提交事物
//            liveBroadcast = new LiveBroadcast();
//            liveBroadcastDao = new LiveBroadcastDaoImpl();
//            result=liveBroadcastDao.deleteLiveBroadcastByKey(key);
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                connection.rollback();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//
//        } finally {
//            JdbcUtil.close(null, null, connection);
//        }
//        return result;
//    }

    @Override
    public int updateLiveState(String key, int status) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            liveBroadcast = new LiveBroadcast();
            result = liveBroadcastDao.updateLiveState(key, status);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public List<LiveBroadcast> getLiveBroadcastList() {
        return liveBroadcastDao.getLiveBroadcastList();
    }

    @Override
    public int auditLiveBroadcast(int[] liveNo) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            liveBroadcast = new LiveBroadcast();
            result = liveBroadcastDao.auditLiveBroadcast(liveNo);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }
    //根据key拿到直播信息

    @Override
    public List<LiveBroadcast> getUserInfoByLiveKey(String liveKey) {
        return liveBroadcastDao.getUserInfoByLiveKey(liveKey);
    }

    @Override
    public int handlingLivestreamingRequests(String userid) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            liveBroadcast = new LiveBroadcast();
            result = liveBroadcastDao.handlingLivestreamingRequests(userid);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }


    @Override
    public int getuUserLiveBroadcastStautsByid(String userId) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            liveBroadcast = new LiveBroadcast();
            result = liveBroadcastDao.getuUserLiveBroadcastStautsByid(userId);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }


    @Override
    public LiveBroadcast getLiveBroadcastInfoByUserId(String userId) {
        return liveBroadcastDao.getLiveBroadcastInfoByUserId(userId);
    }

    @Override
    public int launchLiveBroadcast(LiveBroadcast liveBroadcast) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = liveBroadcastDao.launchLiveBroadcast(liveBroadcast);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }

    @Override
    public int applyforLive(LiveBroadcast liveBroadcast) {
        connection = JdbcUtil.getConnection();
        try {
            connection.setAutoCommit(false);//设置手动提交事物
            result = liveBroadcastDao.applyforLive(liveBroadcast);
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            JdbcUtil.close(null, null, connection);
        }
        return result;
    }
}
